package com.example.elmspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.elmspringboot.controller.utils.Result;
import com.example.elmspringboot.domain.OrderDetail;
import com.example.elmspringboot.service.OrderDetailService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/order-detail")
public class OrderDetailController {
    @Resource
    private OrderDetailService orderDetailService;

    @PostMapping
    public Result addOrderDetail(@RequestBody OrderDetail orderDetail){
        orderDetailService.addOrderDet(orderDetail);
        return new Result(true);
    }
    @GetMapping
    public Result getOrderDetail(@RequestParam Integer orderId){
        return  new Result(true, orderDetailService.getOrderDetailList(orderId));
    }
//    移除订单详情
    @DeleteMapping
    public Result removeOrderDetail(@RequestParam Integer orderId){
        List<OrderDetail> orderDetailList = orderDetailService.list(new QueryWrapper<OrderDetail>().eq("order_id", orderId));
        for (OrderDetail orderDetail : orderDetailList) {
            orderDetailService.removeById(orderDetail.getId());
        }
        return new Result(true);
    }


}
